﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using TBR.Common;

namespace TBR.Repositories
{
    public class AccountRepository
    {
        #region constructor
        public AccountRepository() 
        {
            this.webhelper = new WebHelper();
        }
        #endregion

        #region events
        public delegate void RequestEventHandler(object source, RequestEventArgs args);
        public class RequestEventArgs : EventArgs
        {
            public string HtmlCode;
        }

        #region GoHomePageCompleted
        public event RequestEventHandler GoHomePageCompleted;
        public void OnGoHomePageCompleted()
        {
            if (GoHomePageCompleted != null)
            {
                RequestEventArgs args = new RequestEventArgs();
                args.HtmlCode = HtmlResponse;
                GoHomePageCompleted(this, args);
            }
        }
        private void webhelper_GoHomePageCompleted(object sender, TBR.Common.WebHelper.RequestEventArgs args)
        {
            webhelper.RequestCompleted -= webhelper_GoHomePageCompleted;
            this.HtmlResponse = args.HtmlCode;
            OnGoHomePageCompleted();
        }
        #endregion

        #region AuthorizeCompleted
        public event RequestEventHandler AuthorizeCompleted;
        public void OnAuthorizeCompleted()
        {
            if (AuthorizeCompleted != null)
            {
                RequestEventArgs args = new RequestEventArgs();
                args.HtmlCode = HtmlResponse;
                AuthorizeCompleted(this, args);
            }
        }
        private void webhelper_AuthorizeCompleted(object sender, TBR.Common.WebHelper.RequestEventArgs args)
        {
            webhelper.RequestCompleted -= webhelper_AuthorizeCompleted;
            this.HtmlResponse = args.HtmlCode;
            OnAuthorizeCompleted();
        }
        #endregion

        #endregion

        #region properties
        private WebHelper webhelper { get; set; }
        private string HtmlResponse { get; set; }
        #endregion

        #region public methods
        public void GoHomePage()
        {
            webhelper.RequestCompleted += webhelper_GoHomePageCompleted;
            webhelper.GetHtmlCode(new Uri("http://tabor.ru"), WebHelper.Method.Get);
        }

        public void Authorize(string login, string password)
        {
            Dictionary<string, string> dicPostParams = new Dictionary<string, string>();
            dicPostParams.Add("login", login);
            dicPostParams.Add("password", password);
            dicPostParams.Add("sid", "");
            Cookie cookie = new Cookie("registered1", "yes");
            webhelper.RequestCompleted += webhelper_AuthorizeCompleted;
            webhelper.GetHtmlCode(new Uri("http://tabor.ru/main.php?sid="), WebHelper.Method.Post, dicPostParams, cookie, true);
        }
        #endregion
    }
}
